package com.iplatform.file.function.meta.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 读取python文件
 *
 * @author Dair
 */
public class Java2Python {
	
	private final static String PYTHON_FLAG = "python";
	
	private final static String PYTHON_FILE_URL = "D:\\python_file\\python_demo.py";
	
	public static String exec(String[] arrString) {
		if (arrString == null || arrString.length == 0) {
			return "";
		}
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader in = null;
		try {
			//设置命令行传入参数
			String[] arr = new String[arrString.length + 2];
			arr[0] = PYTHON_FLAG;
			arr[1] = PYTHON_FILE_URL;
			for (int i = 2; i < arr.length; i++) {
				arr[i] = arrString[i - 2];
			}
			Process process = Runtime.getRuntime().exec(arr);
			in = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
			String line;
			while ((line = in.readLine()) != null) {
				stringBuilder.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return stringBuilder.toString();
	}
	
	
}
